//
//  QueryListViewController.m
//  Audit
//
//  Created by cc on 15-2-28.
//  Copyright (c) 2015年 cc. All rights reserved.
//

#import "QueryListViewController.h"
#import "QueryListCell.h"
#import "UIScrollView+SVInfiniteScrolling.h"
#import "UIScrollView+SVPullToRefresh.h"

@interface QueryListViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    
}
@property(nonatomic,retain) IBOutlet UITableView *queryTableList;
@property (nonatomic, assign) NSInteger currentPage;
@property(nonatomic,retain) NSMutableArray *tableDataList;//数据
@end

@implementation QueryListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self setNavTitle:@"车辆信息列表"];
    [self setNavBackButton:nil];
    [self setNavRightButton:@"关闭"];
    
    
    __weak __typeof(&*self)weakSelf = self;
    
    weakSelf.currentPage = 0;
    weakSelf.tableDataList = [NSMutableArray array];

    [self.queryTableList addPullToRefreshWithActionHandler:^{
        weakSelf.currentPage = 0;
        [weakSelf.tableDataList removeAllObjects];
        //todo  添加获取数据请求
        [weakSelf loadMoreData];
    }];
    [weakSelf loadMoreData];
}

-(void)loadMoreData
{
    NSMutableArray *valueArr = [NSMutableArray arrayWithObjects:self.carCondition.vin,self.carCondition.model,self.carCondition.csn,self.carCondition.plant,self.carCondition.domain,self.carCondition.auditGroup,self.carCondition.shiftDate,self.carCondition.buildDate,self.carCondition.series,self.carCondition.phase,self.carCondition.engine,self.carCondition.transmission,self.carCondition.color,self.carCondition.drivedirection,self.carCondition.defect_info,self.carCondition.repair_info, nil];
    
    NSMutableArray *keyArr = [NSMutableArray array];
    
    if (valueArr.count==3) {
        keyArr   = [NSMutableArray arrayWithObjects:@"vin",@"modelList",@"csn", nil];
    }
    else
    {
        keyArr   = [NSMutableArray arrayWithObjects:@"vin",@"modelList",@"csn",@"plantList",@"domainList",@"auditGroupList",@"shiftDate",@"buildDate",@"seriesList",@"phaseList",@"engine",@"transmission",@"color",@"drivedirection",@"defect_info",@"repair_info", nil];
    }

    
    [valueArr addObject:@"10"];
    [keyArr   addObject:@"pageSize"];
    
    [valueArr addObject:[NSString stringWithFormat:@"%ld",(long)self.currentPage]];
    [keyArr   addObject:@"currentPage"];
    
    
    [self showWait:@"" view:self.view];
    
    [CommunicateWithService communicateWithService:keyArr value:valueArr apiName:@"queryVhc.do" finishSelector:@selector(QueryCarFinished:) failSelector:@selector(QueryCarFailed:) delegateObj:self];
}

-(void)QueryCarFinished:(ASIHTTPRequest *)request
{
    [self hideWait];
    if (request.responseStatusCode == 400) {
        //textView.text = @"Invalid code";
        NSLog(@"Invalid code");
    } else if (request.responseStatusCode == 403) {
        NSLog(@"Code already used");
        //textView.text = @"Code already used";
    } else if (request.responseStatusCode == 200) {
        NSDictionary *strDic=[CommunicateWithService getDataDic:request.responseString];//解析json 2 dic
        
        NSString *flag=[strDic objectForKey:@"resultCode"];//服务器返回代码
        
        NSString *msg=[strDic objectForKey:@"resultValue"];//服务器返回提示消息
        
        NSDictionary *strDicData=(NSDictionary *)[strDic objectForKey:@"resultData"];//服务器返回数据字典
        
//        self.tableDataList = [NSMutableArray array];
        
        if ([flag isEqualToString:@"1001"]) {
            NSArray *carList = [strDicData objectForKey:@"content"];
            
            self.pageCount = [[strDicData objectForKey:@"countPage"] integerValue];
            
            
            __weak __typeof(&*self)weakSelf = self;
            
            [weakSelf.queryTableList.pullToRefreshView stopAnimating];
            [weakSelf.queryTableList.infiniteScrollingView stopAnimating];
            
            for (int i = 0; i < carList.count; i++) {
                Car *carobj = [[Car alloc] initWithInfo:(NSDictionary *)[carList objectAtIndex:i]];
                
                [self.tableDataList addObject:carobj];
            }
            
            [self.queryTableList reloadData];
            
            if (weakSelf.pageCount > weakSelf.currentPage) {
                [self.queryTableList addInfiniteScrollingWithActionHandler:^{
//                    weakSelf.isShowHUD = NO;
                    weakSelf.currentPage++;
                    [weakSelf loadMoreData];
                    
                    
                }];
            }
            else
            {
                weakSelf.queryTableList.showsInfiniteScrolling = NO;
            }
            
            
        }
        else if ([flag isEqualToString:@"1002"])
        {
            [self alertError:msg view:self.view animated:YES afterDelay:1.5];
        }
    }
}

-(void)QueryCarFailed:(ASIHTTPRequest *)request
{
    [self hideWait];
    [NetworkCheck checkNetwork:self];
    
    __weak __typeof(&*self)weakSelf = self;
    [weakSelf.queryTableList.pullToRefreshView stopAnimating];
    [weakSelf.queryTableList.infiniteScrollingView stopAnimating];
}

- (void)navRightButtonActionAtTag:(NSUInteger)tag
{
    //todo:返回数据
    [super backAction:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark- UItableview delegate&datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableDataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
//    //车辆列表
    QueryListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QueryListCell"];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"QueryListCell" owner:self options:nil] lastObject];
    }
    
    if (self.tableDataList.count>0) {
        cell.labelSeries.text = ((Car *)[self.tableDataList objectAtIndex:[indexPath row]]).seriesDesc;//生产车型
        cell.labelModel.text = ((Car *)[self.tableDataList objectAtIndex:[indexPath row]]).modelDesc;//生产配置
        cell.VIN.text = ((Car *)[self.tableDataList objectAtIndex:[indexPath row]]).vin;//vin
    }
    
//
//    Car *cellCar=[self.listCar objectAtIndex:[indexPath row]];
//    
//    cell.carType.text = cellCar.seriesDesc;
//    cell.carConfig.text = cellCar.modelDesc;
//    cell.carVIN.text = cellCar.vin;
//    
//    //        cell.carType.text = @"11";
//    //        cell.carConfig.text = @"11";
//    //        cell.carVIN.text = @"11";
//    cell.tag = 10000 + indexPath.row;
//    cell.delegate = self;
//    [cell.extendBtn addTarget:self action:@selector(extendCell:) forControlEvents:UIControlEventTouchUpInside];
//    
//    //        UILongPressGestureRecognizer * longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
//    //        longPressGr.minimumPressDuration = 2.0;
//    //        [cell addGestureRecognizer:longPressGr];
//    
    return cell;

}

// 长按进入详情页
//- (void)tableViewCellDidLongPressed:(LCSwipeTableViewCell *)cell
//{
//    self.selectedCar = [self.listCar objectAtIndex:cell.tag-10000];
//    
//    CarDetailViewController *vc = [[CarDetailViewController alloc] init];
//    vc.selectedCar = self.selectedCar;
//    [self presentViewControllerInFullRect:vc];
//    
//}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 122;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (tableView == _carsTableView) {
//        self.selectedCar = [self.listCar objectAtIndex:[indexPath row]];
//        
//        for (int i=0; i<[_carsTableView.visibleCells count]; i++) {
//            if (i == [indexPath row]) {
//                LCSwipeTableViewCell *cell=[_carsTableView.visibleCells objectAtIndex:[indexPath row]];
//                
//                [cell setSelected:YES];
//            }
//            else
//            {
//                LCSwipeTableViewCell *cell=[_carsTableView.visibleCells objectAtIndex:i];
//                
//                [cell setSelected:NO];
//            }
//        }
//        
//        //        CarDetailViewController *vc = [[CarDetailViewController alloc] init];
//        //        vc.selectedCar = self.selectedCar;
//        //        [self presentViewControllerInFullRect:vc];
//    }
//    else
//    {
//        self.selectedDefect = [self.selectedCar.listDefect objectAtIndex:[indexPath row]];
//    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (tableView == _carsTableView) {
//        self.selectedCar = [self.listCar objectAtIndex:[indexPath row]];
//        
//        for (int i=0; i<[_carsTableView.visibleCells count]; i++) {
//            if (i == [indexPath row]) {
//                LCSwipeTableViewCell *cell=[_carsTableView.visibleCells objectAtIndex:[indexPath row]];
//                
//                [cell setSelected:YES];
//            }
//            else
//            {
//                LCSwipeTableViewCell *cell=[_carsTableView.visibleCells objectAtIndex:i];
//                
//                [cell setSelected:NO];
//            }
//        }
//        
//        //        CarDetailViewController *vc = [[CarDetailViewController alloc] init];
//        //        vc.selectedCar = self.selectedCar;
//        //        [self presentViewControllerInFullRect:vc];
//    }
//    else
//    {
//        self.selectedDefect = [self.selectedCar.listDefect objectAtIndex:[indexPath row]];
//    }
}




@end
